// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NavigationPaneAdapter.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Enables use of a Ribbon control as a Prism region.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Common.Adapters
{
    using System.Collections.Specialized;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using Hisaab.Client.Common.Commands;
    using Microsoft.Practices.Prism.Regions;
    using Stema.Controls;

    /// <summary>
    /// Enables use of a Ribbon control as a Prism region.
    /// </summary>
    /// <remarks> See Developer's Guide to Microsoft Prism (Ver. 4), p. 189.</remarks>
    public class NavigationPaneAdapter : RegionAdapterBase<NavigationPane>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="NavigationPaneAdapter"/> class.
        /// </summary>
        /// <param name="behaviorFactory">
        /// Allows the registration of the default set of RegionBehaviors.
        /// </param>
        public NavigationPaneAdapter(IRegionBehaviorFactory behaviorFactory)
            : base(behaviorFactory)
        {
        }

        /// <summary>
        /// Adapts a WPF control to serve as a Prism IRegion.
        /// </summary>
        /// <param name="region">The new region being used.</param>
        /// <param name="regionTarget">The WPF control to adapt.</param>
        protected override void Adapt(IRegion region, NavigationPane regionTarget)
        {
            region.Views.CollectionChanged += (sender, e) =>
                {
                    switch (e.Action)
                    {
                        case NotifyCollectionChangedAction.Add:
                            foreach (FrameworkElement element in e.NewItems)
                            {
                                var item = new NavigationPaneItem { Content = element, DataContext = element.DataContext };

                                var headerBinding = new Binding("Title");
                                item.SetBinding(HeaderedContentControl.HeaderProperty, headerBinding);

                                var imageBinding = new Binding("Image");
                                item.SetBinding(NavigationPaneItem.ImageProperty, imageBinding);

                                var imageSmallBinding = new Binding("ImageSmall");
                                item.SetBinding(NavigationPaneItem.ImageSmallProperty, imageSmallBinding);

                                var commandBinding = new Binding("SelectedCommand");
                                item.SetBinding(NavigationItemSelectCommand.SelectedCommandProperty, commandBinding);

                                var value = element.GetValue(Helpers.IndexHelper.IndexProperty);

                                if (value != null)
                                {
                                    int controlInsertIndex = int.Parse(value.ToString());

                                    item.SetValue(Helpers.IndexHelper.IndexProperty, controlInsertIndex);

                                    int insertIndex = -1;
                                    int i = 0;
                                    foreach (FrameworkElement control in regionTarget.Items)
                                    {
                                        var itemIndex = control.GetValue(Helpers.IndexHelper.IndexProperty);
                                        if (itemIndex != null)
                                        {
                                            int itemIndexValue = int.Parse(itemIndex.ToString());
                                            if (itemIndexValue > controlInsertIndex)
                                            {
                                                insertIndex = i;
                                                break;
                                            }

                                            i++;
                                        }
                                    }

                                    if (insertIndex >= 0 && insertIndex < regionTarget.Items.Count)
                                    {
                                        regionTarget.Items.Insert(insertIndex, item);
                                        continue;
                                    }

                                    regionTarget.Items.Add(item);
                                }
                            }

                            break;

                        case NotifyCollectionChangedAction.Remove:
                            break;
                    }
                };
        }

        /// <summary>
        /// Creates the region.
        /// </summary>
        /// <returns>Region object</returns>
        protected override IRegion CreateRegion()
        {
            return new SingleActiveRegion();
        }
    }
}